//
//  ITImagePicker.m
//  ImageClipTool
//
//  Created by Seven on 16/7/6.
//  Copyright © 2016年 Seven. All rights reserved.
//

#import "ITImagePicker.h"
#import "ITCropImageController.h"
#import "ITUploadManager.h"
#define ScreenWidth  CGRectGetWidth([UIScreen mainScreen].bounds)
#define ScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@interface ITImagePicker()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
    BOOL isScale;
    double _scale;
}
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
@property (nonatomic, strong) ITCropImageController *imageCropperController;

@property(nonatomic, assign) ITImageScene scene;
@end
@implementation ITImagePicker
#pragma  mark -- 单例 --
+ (instancetype)sharedInstance{
    static dispatch_once_t ETToken;
    static ITImagePicker *sharedInstance = nil;
    dispatch_once(&ETToken, ^{
        sharedInstance = [[ITImagePicker alloc] init];
    });
    return sharedInstance;
}

- (void)showOriginalImagePickerWithType:(ImagePickerType)type inViewController:(UIViewController *)viewController{
    if (type == ImagePickerCamera) {
        self.imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    }else{
        self.imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    }
    isScale = NO;
    self.imagePickerController.allowsEditing = YES;
    _imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
    [viewController presentViewController:_imagePickerController animated:YES completion:nil];
}

- (void)showImagePickerWithType:(ImagePickerType)type inViewController:(UIViewController *)viewController scene:(ITImageScene)scene{
    self.scene = scene;
    if (type == ImagePickerCamera) {
        self.imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    }else{
        self.imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.imagePickerController.allowsEditing = NO;
    isScale = YES;
    
    _imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
    [viewController presentViewController:_imagePickerController animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageOrientation imageOrientation=image.imageOrientation;
    if(imageOrientation!=UIImageOrientationUp){
        // Adjust picture Angle
        UIGraphicsBeginImageContext(image.size);
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
    }
    
    CGFloat ratio = 1;
    BOOL isCycle = NO;
    
    switch (_scene) {
        case ITImageScenePortrait:{
            isCycle = YES;
        }
            break;
        case ITImageSceneLink:{
            //ratio = 80.0/48.0;
            ratio = InteractionLinkImageAspect;
        }
            break;
        case ITImageScenePost:{
            ratio = 0;
        }
            break;
            
        default:
            break;
    }
    
    self.imageCropperController = [[ITCropImageController alloc] initWithImage:image cropRatio:ratio isCycle:isCycle];
    
    __weak typeof(self) weakself = self;
    [_imageCropperController setSubmitblock:^(UIViewController *viewController , UIImage *image) {
        [viewController dismissViewControllerAnimated:NO completion:nil];
        [picker dismissViewControllerAnimated:YES completion:nil];
        if (weakself.delegate) {
            [weakself.delegate imagePicker:weakself didFinished:image];
        }
    }];
    
    [_imageCropperController setCancelblock:^(UIViewController *viewController){
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            [viewController dismissViewControllerAnimated:NO completion:nil];
            [picker dismissViewControllerAnimated:YES completion:nil];
        }else{
            [viewController dismissViewControllerAnimated:YES completion:^{
                
            }];
            [UIApplication sharedApplication].statusBarStyle = [AppUtils getDarkStyle];
        }
    }];
    
    _imageCropperController.modalPresentationStyle = UIModalPresentationFullScreen;
    [picker presentViewController:_imageCropperController animated:YES completion:^{
        
    }];
    //[picker pushViewController:self.imageCropperController animated:YES];
    
//    if (isScale) {
//
//    }else{
//        [picker dismissViewControllerAnimated:YES completion:^{}];
//        [self.delegate imagePicker:self didFinished:image];
//    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    if (self.delegate) {
        [self.delegate imagePickerDidCancel:self];
    }
}
#pragma mark - Getters
- (UIImagePickerController *)imagePickerController{
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        _imagePickerController.allowsEditing = NO;
    }
    return _imagePickerController;
}
@end
